package com.icewld.meiflowerway;

import android.content.Context;

import java.io.Serializable;

class MainCalModel implements Serializable{
    private int fNum,         sNum;
    private int root_upGua,   root_downGua;
    private int doing_upGua,  doing_downGua;
    private int change_upGua, change_downGua;

    MainCalModel(int f, int s){
        fNum = f; sNum = s;
    }

    void GetGua(){
        int upGua = fNum % 8; int downGua = sNum % 8;
        if (upGua == 0){upGua = 8;}
        if (downGua == 0){downGua = 8;}
        root_upGua = upGua; root_downGua = downGua;
        int[] root_upYao = GetYao(root_upGua);
        int[] root_downYao = GetYao(root_downGua);
        int[] doing_upYao = new int[3]; int[] doing_downYao = new int[3];
        for (int i = 0; i < 3; i++){
            if ((i+1) != 3){doing_upYao[i] = root_upYao[i+1];}
            else {doing_upYao[i] = root_downYao[0];}
            if (i == 0){doing_downYao[i] = root_upYao[2];}
            else {doing_downYao[i] = root_downYao[i-1];}
        }
        doing_upGua = SetYao(doing_upYao); doing_downGua = SetYao(doing_downYao);
        int change  = (root_upGua + root_downGua) % 6; if (change == 0) {change = 6;}
        if (change <= 3) {
            if (root_downYao[3-change] == 0) {root_downYao[3-change] = 1;}
            else{root_downYao[3-change] = 0;}
        }
        else{
            if (root_upYao[6-change] == 0) {root_upYao[6-change] = 1;}
            else {root_upYao[6-change] = 0;}
        }
        change_upGua = SetYao(root_upYao);
        change_downGua = SetYao(root_downYao);
    }

    String[] ReadNameAndYao(int enumGua, Context ct){
        int upGua, downGua;
        String name = "Error", yao_word = "Error";
        String[] value  = new String[2];
        if (enumGua == 1){
            upGua   = root_upGua;
            downGua = root_downGua;
        }
        else if (enumGua == 2){
            upGua   = doing_upGua;
            downGua = doing_downGua;
        }
        else{
            upGua   = change_upGua;
            downGua = change_downGua;
        }
        switch (upGua){
            case 1:
                switch (downGua){
                    case 1:
                        name     = ct.getResources().getString(R.string.gua_name_1);
                        yao_word = ct.getResources().getString(R.string.gua_1);
                        break;
                    case 2:
                        name     = ct.getResources().getString(R.string.gua_name_2);
                        yao_word = ct.getResources().getString(R.string.gua_2);
                        break;
                    case 3:
                        name = ct.getResources().getString(R.string.gua_name_3);
                        yao_word = ct.getResources().getString(R.string.gua_3);
                        break;
                    case 4:
                        name = ct.getResources().getString(R.string.gua_name_4);
                        yao_word = ct.getResources().getString(R.string.gua_4);
                        break;
                    case 5:
                        name = ct.getResources().getString(R.string.gua_name_5);
                        yao_word = ct.getResources().getString(R.string.gua_5);
                        break;
                    case 6:
                        name = ct.getResources().getString(R.string.gua_name_6);
                        yao_word = ct.getResources().getString(R.string.gua_6);
                        break;
                    case 7:
                        name = ct.getResources().getString(R.string.gua_name_7);
                        yao_word = ct.getResources().getString(R.string.gua_7);
                        break;
                    case 8:
                        name = ct.getResources().getString(R.string.gua_name_8);
                        yao_word = ct.getResources().getString(R.string.gua_8);
                        break;
                }
                break;
            case 2:
                switch (downGua){
                    case 1:
                        name = ct.getResources().getString(R.string.gua_name_9);
                        yao_word = ct.getResources().getString(R.string.gua_9);
                        break;
                    case 2:
                        name = ct.getResources().getString(R.string.gua_name_10);
                        yao_word = ct.getResources().getString(R.string.gua_10);
                        break;
                    case 3:
                        name = ct.getResources().getString(R.string.gua_name_11);
                        yao_word = ct.getResources().getString(R.string.gua_11);
                        break;
                    case 4:
                        name = ct.getResources().getString(R.string.gua_name_12);
                        yao_word = ct.getResources().getString(R.string.gua_12);
                        break;
                    case 5:
                        name = ct.getResources().getString(R.string.gua_name_13);
                        yao_word = ct.getResources().getString(R.string.gua_13);
                        break;
                    case 6:
                        name = ct.getResources().getString(R.string.gua_name_14);
                        yao_word = ct.getResources().getString(R.string.gua_14);
                        break;
                    case 7:
                        name = ct.getResources().getString(R.string.gua_name_15);
                        yao_word = ct.getResources().getString(R.string.gua_15);
                        break;
                    case 8:
                        name = ct.getResources().getString(R.string.gua_name_16);
                        yao_word = ct.getResources().getString(R.string.gua_16);
                        break;
                }
                break;
            case 3:
                switch (downGua){
                    case 1:
                        name = ct.getResources().getString(R.string.gua_name_17);
                        yao_word = ct.getResources().getString(R.string.gua_17);
                        break;
                    case 2:
                        name = ct.getResources().getString(R.string.gua_name_18);
                        yao_word = ct.getResources().getString(R.string.gua_18);
                        break;
                    case 3:
                        name = ct.getResources().getString(R.string.gua_name_19);
                        yao_word = ct.getResources().getString(R.string.gua_19);
                        break;
                    case 4:
                        name = ct.getResources().getString(R.string.gua_name_20);
                        yao_word = ct.getResources().getString(R.string.gua_20);
                        break;
                    case 5:
                        name = ct.getResources().getString(R.string.gua_name_21);
                        yao_word = ct.getResources().getString(R.string.gua_21);
                        break;
                    case 6:
                        name = ct.getResources().getString(R.string.gua_name_22);
                        yao_word = ct.getResources().getString(R.string.gua_22);
                        break;
                    case 7:
                        name = ct.getResources().getString(R.string.gua_name_23);
                        yao_word = ct.getResources().getString(R.string.gua_23);
                        break;
                    case 8:
                        name = ct.getResources().getString(R.string.gua_name_24);
                        yao_word = ct.getResources().getString(R.string.gua_24);
                        break;
                }
                break;
            case 4:
                switch (downGua){
                    case 1:
                        name = ct.getResources().getString(R.string.gua_name_25);
                        yao_word = ct.getResources().getString(R.string.gua_25);
                        break;
                    case 2:
                        name = ct.getResources().getString(R.string.gua_name_26);
                        yao_word = ct.getResources().getString(R.string.gua_26);
                        break;
                    case 3:
                        name = ct.getResources().getString(R.string.gua_name_27);
                        yao_word = ct.getResources().getString(R.string.gua_27);
                        break;
                    case 4:
                        name = ct.getResources().getString(R.string.gua_name_28);
                        yao_word = ct.getResources().getString(R.string.gua_28);
                        break;
                    case 5:
                        name = ct.getResources().getString(R.string.gua_name_29);
                        yao_word = ct.getResources().getString(R.string.gua_29);
                        break;
                    case 6:
                        name = ct.getResources().getString(R.string.gua_name_30);
                        yao_word = ct.getResources().getString(R.string.gua_30);
                        break;
                    case 7:
                        name = ct.getResources().getString(R.string.gua_name_31);
                        yao_word = ct.getResources().getString(R.string.gua_31);
                        break;
                    case 8:
                        name = ct.getResources().getString(R.string.gua_name_32);
                        yao_word = ct.getResources().getString(R.string.gua_32);
                        break;
                }
                break;
            case 5:
                switch (downGua){
                    case 1:
                        name = ct.getResources().getString(R.string.gua_name_33);
                        yao_word = ct.getResources().getString(R.string.gua_33);
                        break;
                    case 2:
                        name = ct.getResources().getString(R.string.gua_name_34);
                        yao_word = ct.getResources().getString(R.string.gua_34);
                        break;
                    case 3:
                        name = ct.getResources().getString(R.string.gua_name_35);
                        yao_word = ct.getResources().getString(R.string.gua_35);
                        break;
                    case 4:
                        name = ct.getResources().getString(R.string.gua_name_36);
                        yao_word = ct.getResources().getString(R.string.gua_36);
                        break;
                    case 5:
                        name = ct.getResources().getString(R.string.gua_name_37);
                        yao_word = ct.getResources().getString(R.string.gua_37);
                        break;
                    case 6:
                        name = ct.getResources().getString(R.string.gua_name_38);
                        yao_word = ct.getResources().getString(R.string.gua_38);
                        break;
                    case 7:
                        name = ct.getResources().getString(R.string.gua_name_39);
                        yao_word = ct.getResources().getString(R.string.gua_39);
                        break;
                    case 8:
                        name = ct.getResources().getString(R.string.gua_name_40);
                        yao_word = ct.getResources().getString(R.string.gua_40);
                        break;
                }
                break;
            case 6:
                switch (downGua){
                    case 1:
                        name = ct.getResources().getString(R.string.gua_name_41);
                        yao_word = ct.getResources().getString(R.string.gua_41);
                        break;
                    case 2:
                        name = ct.getResources().getString(R.string.gua_name_42);
                        yao_word = ct.getResources().getString(R.string.gua_42);
                        break;
                    case 3:
                        name = ct.getResources().getString(R.string.gua_name_43);
                        yao_word = ct.getResources().getString(R.string.gua_43);
                        break;
                    case 4:
                        name = ct.getResources().getString(R.string.gua_name_44);
                        yao_word = ct.getResources().getString(R.string.gua_44);
                        break;
                    case 5:
                        name = ct.getResources().getString(R.string.gua_name_45);
                        yao_word = ct.getResources().getString(R.string.gua_45);
                        break;
                    case 6:
                        name = ct.getResources().getString(R.string.gua_name_46);
                        yao_word = ct.getResources().getString(R.string.gua_46);
                        break;
                    case 7:
                        name = ct.getResources().getString(R.string.gua_name_47);
                        yao_word = ct.getResources().getString(R.string.gua_47);
                        break;
                    case 8:
                        name = ct.getResources().getString(R.string.gua_name_48);
                        yao_word = ct.getResources().getString(R.string.gua_48);
                        break;
                }
                break;
            case 7:
                switch (downGua){
                    case 1:
                        name = ct.getResources().getString(R.string.gua_name_49);
                        yao_word = ct.getResources().getString(R.string.gua_49);
                        break;
                    case 2:
                        name = ct.getResources().getString(R.string.gua_name_50);
                        yao_word = ct.getResources().getString(R.string.gua_50);
                        break;
                    case 3:
                        name = ct.getResources().getString(R.string.gua_name_51);
                        yao_word = ct.getResources().getString(R.string.gua_51);
                        break;
                    case 4:
                        name = ct.getResources().getString(R.string.gua_name_52);
                        yao_word = ct.getResources().getString(R.string.gua_52);
                        break;
                    case 5:
                        name = ct.getResources().getString(R.string.gua_name_53);
                        yao_word = ct.getResources().getString(R.string.gua_53);
                        break;
                    case 6:
                        name = ct.getResources().getString(R.string.gua_name_54);
                        yao_word = ct.getResources().getString(R.string.gua_54);
                        break;
                    case 7:
                        name = ct.getResources().getString(R.string.gua_name_55);
                        yao_word = ct.getResources().getString(R.string.gua_55);
                        break;
                    case 8:
                        name = ct.getResources().getString(R.string.gua_name_56);
                        yao_word = ct.getResources().getString(R.string.gua_56);
                        break;
                }
                break;
            case 8:
                switch (downGua){
                    case 1:
                        name = ct.getResources().getString(R.string.gua_name_57);
                        yao_word = ct.getResources().getString(R.string.gua_57);
                        break;
                    case 2:
                        name = ct.getResources().getString(R.string.gua_name_58);
                        yao_word = ct.getResources().getString(R.string.gua_58);
                        break;
                    case 3:
                        name = ct.getResources().getString(R.string.gua_name_59);
                        yao_word = ct.getResources().getString(R.string.gua_59);
                        break;
                    case 4:
                        name = ct.getResources().getString(R.string.gua_name_60);
                        yao_word = ct.getResources().getString(R.string.gua_60);
                        break;
                    case 5:
                        name = ct.getResources().getString(R.string.gua_name_61);
                        yao_word = ct.getResources().getString(R.string.gua_61);
                        break;
                    case 6:
                        name = ct.getResources().getString(R.string.gua_name_62);
                        yao_word = ct.getResources().getString(R.string.gua_62);
                        break;
                    case 7:
                        name = ct.getResources().getString(R.string.gua_name_63);
                        yao_word = ct.getResources().getString(R.string.gua_63);
                        break;
                    case 8:
                        name = ct.getResources().getString(R.string.gua_name_64);
                        yao_word = ct.getResources().getString(R.string.gua_64);
                        break;
                }
                break;
        }
        value[0] = name; value[1] = yao_word;
        return value;
    }

    public int[] GetYao(int gua){
        //根据卦的编号输出三爻的阴阳
        //输入：上卦下卦的编号
        //输出：三爻的阴阳，阴为0，阳为1
        int[] yao = new int[3];
        switch (gua){
            case 1:
                yao[0] = 1; yao[1] = 1; yao[2] = 1;
                break;
            case 2:
                yao[0] = 0; yao[1] = 1; yao[2] = 1;
                break;
            case 3:
                yao[0] = 1; yao[1] = 0; yao[2] = 1;
                break;
            case 4:
                yao[0] = 0; yao[1] = 0; yao[2] = 1;
                break;
            case 5:
                yao[0] = 1; yao[1] = 1; yao[2] = 0;
                break;
            case 6:
                yao[0] = 0; yao[1] = 1; yao[2] = 0;
                break;
            case 7:
                yao[0] = 1; yao[1] = 0; yao[2] = 0;
                break;
            case 8:
                yao[0] = 0; yao[1] = 0; yao[2] = 0;
                break;
        }
        return yao;
    }

    public int SetYao(int[] yao){
        //根据三爻输出卦的编号
        //输入：三爻的阴阳
        //输出：卦的编号
        if (yao[0] == 1){
            if (yao[1] == 1){
                if (yao[2] == 1){return 1;}
                else{return 5;}
            }
            else{
                if (yao[2] == 1){return 3;}
                else{return 7;}
            }
        }
        else{
            if (yao[1] == 1){
                if (yao[2] == 1){return 2;}
                else{return 6;}
            }
            else{
                if (yao[2] == 1){return 4;}
                else {return 8;}
            }
        }
    }

    public int getRoot_upGua() {
        return root_upGua;
    }

    public int getRoot_downGua() {
        return root_downGua;
    }

    public int getDoing_upGua() {
        return doing_upGua;
    }

    public int getDoing_downGua() {
        return doing_downGua;
    }

    public int getChange_upGua() {
        return change_upGua;
    }

    public int getChange_downGua() {
        return change_downGua;
    }
}
